//
//  SISSRoomChooseViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/8/3.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSRoomChooseViewController.h"
#import "SISSExcelView.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"

@interface SISSRoomChooseViewController ()<SISSExcelViewDelegate,SISSExcelViewDataSource>

/// textField
@property (weak, nonatomic) UITextField *textField;
/// confirmButton
@property (weak, nonatomic) UIButton *confirmButton;
@property (weak, nonatomic) SISSExcelView *excelView;

///
@property (strong, nonatomic) NSArray<SISSRoomFloorModel *> *roomNumberArr;

@end
@implementation SISSRoomChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)setBuildingId:(NSString *)buildingId {
    _buildingId = buildingId;
    [kJsonRequestNetworkingManager requestHouseRoomNumberWithBuilding:buildingId successBlock:^(NSArray<SISSRoomFloorModel *> *roomNumberArr) {
        
        //        self.roomNumberArr = [roomNumberArr sortedArrayUsingComparator:^NSComparisonResult(SISSRoomNumberModel *obj1, SISSRoomNumberModel *obj2) {
        //            // 升序排序,由小到大,便于理解,方便记忆成obj2->obj1,也是降序
        //            return [obj1.floor compare:obj2.floor];
        //        }];
        self.roomNumberArr = roomNumberArr;
        [self.excelView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - SISSExcelViewDelegate & SISSExcelViewDataSource
- (NSString *)contentTableView:(SISSExcelContentTableView *)contentTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withColumn:(NSInteger)column {
    if (column >= self.roomNumberArr[indexPath.item].fanghao.count) {
        return @""; // 数据不一样多的时候
    }
//    NSLog(@"%zd --%@-- %zd",column,self.roomNumberArr[indexPath.item].fanghao[column],self.roomNumberArr[indexPath.item].fanghao.count);
    return self.roomNumberArr[indexPath.item].fanghao[column].dnum;
}

- (NSInteger)contentTableView:(SISSExcelContentTableView *)contentTableView numberOfRowsInSection:(NSInteger)section {
    return self.roomNumberArr.count;
}
- (NSInteger)numberOfColumnInExcelView:(SISSExcelView *)excelView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 0;
    for (SISSRoomFloorModel *model in self.roomNumberArr) {
        if (model.fanghao.count > count) {
            count = model.fanghao.count;
        }
    }
    return count;
}


// 左边序号 内容   默认是 1、2、3、、、、
- (NSString *)leftTableView:(SISSExcelLeftTableView *)leftTableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    return self.roomNumberArr[indexPath.row].floor;
}

- (void)contentTableView:(SISSExcelContentTableView *)contentTableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath withColumn:(NSInteger)column {
    NSLog(@"contentTableView == indexPath.section:%zd -- indexPath.item:%zd --column: %zd",indexPath.section,indexPath.item,column);
    if (self.selectBlock) {
        SISSRoomFloorModel *model = self.roomNumberArr[indexPath.item];
        if (column < model.fanghao.count) {
            NSLog(@"选择了 ： %@",model.fanghao[column]);
            self.selectBlock(model.fanghao[column]);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
- (void)contentTableView:(SISSExcelContentTableView *)contentTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"contentTableView == indexPath.section:%zd -- indexPath.item:%zd",indexPath.section,indexPath.item);
}

- (void)leftTableView:(SISSExcelLeftTableView *)leftTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"leftTableView == indexPath.section:%zd -- indexPath.item:%zd",indexPath.section,indexPath.row);
}

- (void)confirmButtonClickAction {
    if (self.selectBlock && self.textField.text.length > 0) {
        SISSRoomNumberModel *model = [[SISSRoomNumberModel alloc] init];
        model.dnum = self.textField.text;
        if (self.textField.text.length == 0) {
            [self alertWithMessage:@"请输入房号"];
            return;
        }
        self.selectBlock(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = @"请输入房号";
    textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:textField];
    self.textField = textField;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:16];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    //    confirmButton.layer.borderWidth = 2;
    //    confirmButton.layer.borderColor = MAIN_COLOR.CGColor;
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
    SISSExcelView *excelView = [[SISSExcelView alloc] init];
    excelView.delegate = self;
    excelView.dataSource = self;
    excelView.contentWidth = 170;
    excelView.contentHeight = 66;
    [self.view addSubview:excelView];
    self.excelView = excelView;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    WeakSelf
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view).offset(kTopHeight + 2 * margin);
        make.left.equalTo(weakSelf.view).offset(2 * margin);
        make.right.equalTo(weakSelf.confirmButton.mas_left).offset(-2 * margin);
        make.height.mas_equalTo(4 * margin);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.textField);
        make.right.equalTo(weakSelf.view).offset(-2 * margin);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH * 0.15, 4 * margin));
    }];
    [self.excelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.textField.mas_bottom).offset(2 * margin);
    }];
}

#pragma mark - 懒加载
- (NSArray<SISSRoomFloorModel *> *)roomNumberArr {
    if (!_roomNumberArr) {
        _roomNumberArr = [NSArray array];
    }
    return _roomNumberArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
